import json
import urllib2
import time
import datetime

def importEvent(item, tournament):
    print "---EVENT---"
    # Parsing the gender from the 5 options (which I don't really understand, but ok)
    gender = item['gender'].lower()
    if "men" in gender:
        gender = "Men"
    elif "women" in gender:
        gender = "Women"
    elif "mixed" in gender:
        gender = "Mixed"
        
    print "Gender: " + gender
    
    age = item['age_limit'].lower()
    
    if age == "none":
        age = "open"    
    print "Age: " + age
    
    startDate = item['close_of_reg']
    #2012-06-09T12:30:00-04:00 <- Example time object from askfred for reference, need to figure out wth the -04:00 is, maybe timezone, with daylight savings?
    
    # Strip out the weird end part
    startDate = startDate[:startDate.rfind("-")]
    
    # Parse the data and conver it to a form django likes
    startDate = time.strptime(startDate, "%Y-%m-%dT%H:%M:%S")
    start = datetime.datetime.fromtimestamp(time.mktime(startDate))
    print "Start: " + str(start)
    
    weapon = item['weapon']
    print "Weapon: " + weapon
    
    ratingLimit = item['rating_limit'].lower()
    
    upperLimit = "A"
    lowerLimit = "U"
    
    # Translate FRED's rating limits to my own...
    if ratingLimit == "div3":
        upperLimit = "D"
        lowerLimit = "U"
    elif ratingLimit == "div2":
        upperLimit = "C"
        lowerLimit = "U"
    elif ratingLimit == "div1":
        upperLimit = "A"
        lowerLimit = "C"
    elif ratingLimit == "div1a":
        upperLimit = "A"
        lowerLimit = "U"
    elif ratingLimit == "open":
        upperLimit = "A"
        lowerLimit = "U"
    elif ratingLimit == "eunder":
        upperLimit = "E"
        lowerLimit = "U"
    elif ratingLimit == "dabove":
        upperLimit = "A"
        lowerLimit = "D"
    elif ratingLimit == "aonly":
        upperLimit = "A"
        lowerLimit = "A"
    elif ratingLimit == "babove":
        upperLimit = "A"
        lowerLimit = "B"
    elif ratingLimit == "unrated":
        upperLimit = "U"
        lowerLimit = "U"
    
    print "Upper Limit: " + upperLimit
    print "Lower Limit: " + lowerLimit
    
    print "Tournament: " + str(tournament["id"])
    print item

def importTournament(number):
    myAPIKey = "You'll need to get one of these in order to run the script"
    tournamentData = json.load(urllib2.urlopen('https://apisandbox.askfred.net/v1/tournament/'+str(number)+'?_api_key='+myAPIKey))
    eventData = json.load(urllib2.urlopen('https://apisandbox.askfred.net/v1/event/?tournament_id='+str(number)+'&_api_key='+myAPIKey))
    
    eventData2 = json.load(urllib2.urlopen('https://apisandbox.askfred.net/v1/event/?roundresult='+str(number)+'&_api_key='+myAPIKey))
    print eventData2
    tournament = tournamentData['tournament']
    
    print "---Tournament---"
    print "Name: " + tournament['name']
    print "Host: " + "N/A"
    print "City: " + tournament['venue']['city']
    print "State: " + tournament['venue']['state']
    print "Street: " + tournament['venue']['address']
    print "Details: " + tournament['comments']
    
    for item in eventData['events']:
        importEvent(item, tournament)
    
importTournament(18573)
